import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from editor import ScintillaEditor
import ntpath
import globals
from PyQt5.QtWidgets import QApplication, QMainWindow, QFrame, QVBoxLayout, QHBoxLayout, QPushButton, QFileDialog
from PyQt5.QtCore import QPoint, QSize, QRect

'''================================================================================'''
'''|                                TABMASTER                                     |'''
'''================================================================================'''
class TabMaster(QTabWidget):

    def __init__(self):
        super(TabMaster, self).__init__()

    ''''''

    def show_file(self, filepath):
        f = open(filepath, "r")
        text = f.read()
        f.close()

        newtab = ScintillaEditor(text, self.show_symbol)
        newtabName = ntpath.basename(filepath)

        self.addTab(newtab, newtabName)
        self.setCurrentWidget(newtab)

        newtab.setCursorPosition(0, 0)
        newtab.ensureCursorVisible()
        newtab.setFocus()

    ''''''

    def show_symbol(self, filepath, linefocus = 0, colfocus = 0):
        for i in range(self.count()):
            tabname = str(self.tabText(i))
            if tabname == ntpath.basename(filepath):
                # Tab already exists
                self.setCurrentIndex(i)
                self.widget(i).setCursorPosition(linefocus, colfocus)
                self.widget(i).ensureCursorVisible()
                self.widget(i).setFocus()
                return
            ###
        ###

        # Make new tab
        f = open(filepath, "r")
        text = f.read()
        f.close()

        newtab = ScintillaEditor(text, self.show_symbol)
        newtabName = ntpath.basename(filepath)

        self.addTab(newtab, newtabName)
        self.setCurrentWidget(newtab)

        newtab.setCursorPosition(linefocus, colfocus)
        newtab.ensureCursorVisible()
        newtab.setFocus()

        ###

    ''''''

'''=== end class ==='''

def mainwindow_init(self):
    # -------------------------------- #
    #           Window setup           #
    # -------------------------------- #

    # 1. Define the geometry of the main window
    # ------------------------------------------
    # 1. Set the main window size to 30% of the screen size
    desktop = QApplication.desktop()
    screen_size = desktop.screenGeometry(self)
    window_size = QSize(int(screen_size.width() * 0.6), int(screen_size.height() * 0.5))
    self.resize(window_size)

    # 2. Calculate the position to center the window
    center_point = QPoint(screen_size.width() // 2, screen_size.height() // 2)
    window_pos = center_point - QPoint(window_size.width() // 2, window_size.height() // 2)
    self.move(window_pos)
    
    self.setWindowTitle("tinyLog")

'''================================================================================'''
'''|                           CUSTOM MAIN WINDOW                                 |'''
'''================================================================================'''
class CustomMainWindow(QMainWindow):

    def __init__(self):
        super(CustomMainWindow, self).__init__()

        mainwindow_init(self)

        # 2. Create frame and layout
        # ---------------------------
        self.__frm = QFrame(self)
        self.__frm.setStyleSheet("QWidget { background-color: #ffeaeaea }")
        self.__lyt = QVBoxLayout()
        self.__frm.setLayout(self.__lyt)
        self.setCentralWidget(self.__frm)
        self.__myFont = QFont()
        self.__myFont.setPointSize(14)

        # 3. Place a button
        # ------------------
        self.__btn = QPushButton("Hello World")
        self.__btn.setFixedWidth(50)
        self.__btn.setFixedHeight(50)
        self.__btn.clicked.connect(self.__btn_action)
        self.__btn.setFont(self.__myFont)
        self.__lyt.addWidget(self.__btn)

        # 4. Insert the TabMaster
        # ------------------------
        self.__tabMaster = TabMaster()
        self.__tabMaster.show_file(globals.projectMainFile)
        self.__lyt.addWidget(self.__tabMaster)
        self.show()
        
        # 5. Place the open file button
        # -----------------------------
        self.__btnOpenFile = QPushButton("Open File")
        self.__btnOpenFile.setFixedWidth(100)  # 可以根据需要调整按钮宽度
        self.__btnOpenFile.setFont(self.__myFont)
        self.__btnOpenFile.clicked.connect(self.open_file)  # 绑定槽函数
        self.__lyt.addWidget(self.__btnOpenFile)  # 添加到布局

    ''''''
    def open_file(self):
        options = QFileDialog.Options()
        fileName, _ = QFileDialog.getOpenFileName(self, "Open File", "",
                                                  "All Files (*);;Text Files (*.txt)", options=options)
        if fileName:
            # 这里可以添加代码来加载和显示选中的文件
            print(f"Selected file: {fileName}")
            # 例如，你可以使用QScintilla控件来加载和显示文件内容
            # self.__tabMaster.load_file(fileName)  # 假设这是加载文件的方法
                                                  
    def __btn_action(self):
        print("Hello World")

    ''''''

'''=== end class ==='''

if __name__ == '__main__':
    app = QApplication(sys.argv)
    QApplication.setStyle(QStyleFactory.create('Fusion'))
    myGUI = CustomMainWindow()
    sys.exit(app.exec_())

''''''